//防抖
let timeout = null;
export function debounce(func, times = 500, immediate = false) {
	if (timeout !== null) clearTimeout(timeout);
	if (immediate) {
		var callNow = !timeout;
		timeout = setTimeout(function() {
			timeout = null;
		}, times);
		if (callNow) typeof func === 'function' && func();
	} else {
		timeout = setTimeout(function() {
			typeof func === 'function' && func();
		}, times);
	}
}

export function deepClone(obj) {
	if (obj == null || typeof obj !== 'object') {
		return obj;
	}
	switch (Object.prototype.toString.call(obj)) {
		case '[object Array]': {
			var result = new Array(obj.length);
			for (var i = 0; i < result.length; ++i) {
				result[i] = deepClone(obj[i]);
			}
			return result;
		}

		case '[object Error]': {
			var result = new obj.constructor(obj.message);
			result.stack = obj.stack; // hack...
			return result;
		}

		case '[object Date]':
		case '[object RegExp]':
		case '[object Int8Array]':
		case '[object Uint8Array]':
		case '[object Uint8ClampedArray]':
		case '[object Int16Array]':
		case '[object Uint16Array]':
		case '[object Int32Array]':
		case '[object Uint32Array]':
		case '[object Float32Array]':
		case '[object Float64Array]':
		case '[object Map]':
		case '[object Set]':
			return new obj.constructor(obj);

		case '[object Object]': {
			var keys = Object.keys(obj);
			var result = {};
			for (var i = 0; i < keys.length; ++i) {
				var key = keys[i];
				result[key] = deepClone(obj[key]);
			}
			return result;
		}

		default: {
			throw new Error("Unable to copy obj! Its type isn't supported.");
		}
	}
}

//相册授权
export function scopeAlbum(){
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success(res) {
				if (res.authSetting['scope.album'] == false) {
					uni.showModal({
						title: '温馨提示',
						content: '为了更好的体验,请授权相册哦~',
						showCancel: false,
						success() {
							uni.openSetting({
								success(res2) {
									if (res2.authSetting['scope.album']) {
										resolve();
									} else {
										reject('error2');
									}
								},
								fail(err) {
									reject('error2');
								}
							})
						}
					})
				} else {
					resolve();
				}
			},
			fail(err) {
				reject('error');
			}
		})
	}).then(undefined, (error) => {
		throw new Error(error);
	});
}

export function gridData (){
	return [
      {
        id:1,
        text:'二宫格',
        cutNum:2,
        x:2,
        y:1,
		flex:2,
        cutPoint:[
          {
            x:0,
            y:0,
            borderCons:{'border-right':'none'},
          },
          {
            x:1,
            y:0,
            borderCons:{},
          }
        ],
        icon:'../../../static/grid_2.png'
      },
      {
        id:2,
        text:'三宫格',
        cutNum:3,
		flex:3,
        x:3,
        y:1,
        cutPoint:[
          {
            x:0,
            y:0,
            borderCons:{'border-right':'none'},
          },
          {
            x:1,
            y:0,
            borderCons:{'border-right':'none'},

          },
          {
            x:2,
            y:0,
            borderCons:{},

          }
        ],
        icon:'../../../static/grid_3.png'
      },
      {
        id:3,
        text:'四宫格',
        cutNum:4,
		flex:2,
        x:2,
        y:2,
        cutPoint:[
          {
            x:0,
            y:0,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:1,
            y:0,
            borderCons:{'border-bottom':'none'},
          },
          {
            x:0,
            y:1,
            borderCons:{'border-right':'none'},
          },
          {
            x:1,
            y:1,
            borderCons:{},
          }
        ],
        icon:'../../../static/grid_4.png'
      },
      {
        id:4,
        text:'六宫格',
        cutNum:6,
		flex:3,
        x:3,
        y:2,
        cutPoint:[
          {
            x:0,
            y:0,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:1,
            y:0,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:2,
            y:0,
            borderCons:{'border-bottom':'none'},
          },
          {
            x:0,
            y:1,
            borderCons:{'border-right':'none'},
          },
          {
            x:1,
            y:1,
            borderCons:{'border-right':'none'},
          },
          {
            x:2,
            y:1,
            borderCons:{},
          }
        ],
        icon:'../../../static/grid_6.png'
      },
      {
        id:5,
        text:'九宫格',
        cutNum:9,
		flex:3,
        x:3,
        y:3,
        cutPoint:[
          {
            x:0,
            y:0,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:1,
            y:0,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:2,
            y:0,
            borderCons:{'border-bottom':'none'},
          },
          {
            x:0,
            y:1,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:1,
            y:1,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:2,
            y:1,
            borderCons:{'border-bottom':'none'},
          },
          {
            x:0,
            y:2,
            borderCons:{'border-right':'none'},
          },
          {
            x:1,
            y:2,
            borderCons:{'border-right':'none'},
          },
          {
            x:2,
            y:2,
            borderCons:{},
          }
        ],
        icon:'../../../static/grid_9.png'
      },
      {
        id:6,
        text:'十二宫格',
        cutNum:12,
		flex:3,
        x:3,
        y:4,
        cutPoint:[
          {
            x:0,
            y:0,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:1,
            y:0,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:2,
            y:0,
            borderCons:{'border-bottom':'none'},
          },
          {
            x:0,
            y:1,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:1,
            y:1,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:2,
            y:1,
            borderCons:{'border-bottom':'none'},
          },
          {
            x:0,
            y:2,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:1,
            y:2,
            borderCons:{'border-right':'none','border-bottom':'none'},
          },
          {
            x:2,
            y:2,
            borderCons:{'border-bottom':'none'},
          },
          {
            x:0,
            y:3,
            borderCons:{'border-right':'none'},
          },
          {
            x:1,
            y:3,
            borderCons:{'border-right':'none'},
          },
          {
            x:2,
            y:3,
            borderCons:{},
          }
        ],
        icon:'../../../static/grid_12.png'
      }
    ]
}

export function scaleData (){
	return [
		{
		  id:0,
		  text:'铺满',
		  w:1,
		  h:0,
		  wdpr:1,
		  hdpr:0,
		  icon:'../../../static/scale_1.png'
		},
		{
		  id:1,
		  text:'1:1',
		  w:1,
		  h:1,
		  wdpr:1,
		  hdpr:1,
		  icon:'../../../static/scale_1.png'
		},
		{
		  id:2,
		  text:'3:4',
		  w:3,
		  h:4,
		  wdpr:1.3,
		  hdpr:0.75,
		  icon:'../../../static/scale_3.png'
		},
		{
		  id:3,
		  text:'9:16',
		  w:9,
		  h:16,
		  wdpr:1.7777,
		  hdpr:0.5625,
		  icon:'../../../static/scale_9.png'
		}
	]
}
